package com.example.springevent.listener;

import com.example.springevent.event.TransactionalEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 事务性事件监听器 - 使用@TransactionalEventListener注解
 */
@Slf4j
@Component
public class TransactionalEventListener1 {
    
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handleAfterCommit(TransactionalEvent event) {
        log.info("事务提交后处理事件: 操作 = {}, 实体ID = {}", 
                event.getOperation(), 
                event.getEntityId());
    }
    
    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
    public void handleAfterRollback(TransactionalEvent event) {
        log.info("事务回滚后处理事件: 操作 = {}, 实体ID = {}", 
                event.getOperation(), 
                event.getEntityId());
    }
    
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
    public void handleBeforeCommit(TransactionalEvent event) {
        log.info("事务提交前处理事件: 操作 = {}, 实体ID = {}", 
                event.getOperation(), 
                event.getEntityId());
    }
}